package com.example.controller;

import com.example.entity.User;
import com.example.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: luog
 * @Date: 2024/11/27
 */
@Controller
@RequestMapping("/")
@Api(tags = "用户管理页面跳转")
public class UserPageController {

    @Autowired
    private UserService userService;

    @GetMapping("/addUser")
    @ApiOperation(value = "新增用户")
    public String addUser() {
        return "add";
    }

    @GetMapping("/view/{id}")
    @ApiOperation(value = "查看用户")
    public String view(@PathVariable Long id,Model m) {
        buildModle(id, m);
        return "add";
    }
    @GetMapping("/edit/{id}")
    @ApiOperation(value = "编辑用户")
    public String edit(@PathVariable Long id,Model m) {
        buildModle(id, m);
        return "add";
    }

    private void buildModle(Long id, Model m) {
        User byId = userService.getById(id);
        if (byId != null) {
            m.addAttribute("user",byId);
        }
    }

    @GetMapping("/")
    public String getUsers(Model m) {
        // 你的业务逻辑
        // 返回Thymeleaf视图名称
        List<User> users = userService.list();
        m.addAttribute("users", users);
        return "list";
    }
}
